package com.lzy.crm.wechat.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.base.util.BootStrapReturn;
import com.lzy.crm.base.util.JsonUtil;
import com.lzy.crm.base.util.StringUtil;
import com.lzy.crm.domain.AdviceMessage;
import com.lzy.crm.domain.User;
import com.lzy.crm.service.AdviceMessageService;
import com.lzy.crm.service.TradeFlowService;
import com.lzy.crm.service.UserService;
import com.lzy.crm.util.BootJsonResponse;
import com.lzy.crm.wechat.consts.WechatPayProperties;

/**
 * 棋牌项目
 * File: AdviceController.java
 * 
 * Copyright (C): 2017
 *
 * Description:  投诉建议Action
 *
 * @author 陈升平
 * Notes: AdviceController.java 2017-07-03 下午18:56:14 CHENSP
 */
@Controller
@RequestMapping("/wechat")
public class AdviceController {
	/**
	 * logger
	 */
	Logger logger= Logger.getLogger(OrderController.class);
	
	@Resource
	UserService userService;
	
	@Resource
	AdviceMessageService AMService;
	
	@Resource
	TradeFlowService tradeFlowService;
	
	/**
	 * 进入客户建议页
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/advice") 
	public String myAdviceMessage(HttpServletRequest request, ModelMap model) {
		User user =(User)request.getSession().getAttribute("userSession");
		// 订单信息
		if(user==null||StringUtil.isEmpty(user.getOpenid())
				||StringUtil.isEmpty(user.getUnionid())
				){
			// 不构成支付条件
			model.put("appid", WechatPayProperties.getAppId());
			model.put("redirectUri", WechatPayProperties.getRedirectUri()+"?page=wechat/advice.html");
			return "wechat/index2"; 
		}
		return "wechat/advice/advice_info";
	}
	
	/**
	 * 进入客户投诉页
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/complain") 
	public String complainMessage(HttpServletRequest request, ModelMap model) {
		User user =(User)request.getSession().getAttribute("userSession");
		// 订单信息
		if(user==null||StringUtil.isEmpty(user.getOpenid())
				||StringUtil.isEmpty(user.getUnionid())
				){
			// 不构成支付条件
			model.put("appid", WechatPayProperties.getAppId());
			model.put("redirectUri", WechatPayProperties.getRedirectUri()+"?page=wechat/complain.html");
			return "wechat/index2"; 
		}
		return "wechat/advice/complain_info";
	}
	
	/**
	 * 添加客户投诉建议
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/add_advice_message", method=RequestMethod.POST, produces="text/html;charset=UTF-8")
	@ResponseBody
	public String addAdviceMessage(HttpServletRequest request,ModelMap model) {
		BootJsonResponse response=new BootJsonResponse();
		User user =(User)request.getSession().getAttribute("userSession");
		
		AdviceMessage AM = new AdviceMessage();
		try {
			AM.setUserId(user.getId());
			AM.setMsg(request.getParameter("msg"));
			AM.setType(request.getParameter("type"));
			AM.setGameId(user.getGameUserId());
			AM.setCreateTime(new Date());
			AM.setStatus("0");
			
			Integer id=AMService.insertSelective(AM);
			System.out.println("id:"+id);
			response.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
			response.setMessage("操作成功，谢谢你的宝贵意见~ ");
			logger.info("操作成功");
		}catch (Exception e) {
			response.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			response.setMessage("操作失败，请联系管理员~");
			logger.error(" 成为代理商错误 "+e);
		}
		
		return JsonUtil.toJson(response);
	}
	
	/**
	 * 回复：客户投诉建议
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/update_advice_message", method=RequestMethod.POST, produces="text/html;charset=UTF-8")
	@ResponseBody
	public String updateAdviceMessage(HttpServletRequest request,ModelMap model) {
		BootJsonResponse response=new BootJsonResponse();
		User user =(User)request.getSession().getAttribute("userSession");
		
		AdviceMessage AM = new AdviceMessage();
		try {
			AM.setReplyId(user.getId());
			AM.setReplyMsg(request.getParameter("reply_msg"));
			AM.setType(request.getParameter("type"));
			AM.setReplyName(request.getParameter("reply_name"));
			AM.setReplyTime(new Date());
			AM.setStatus("1");
			
			AMService.updateByPrimaryKeySelective(AM);
			
			response.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
			response.setMessage("操作成功");
			logger.info("操作成功");
		}catch (Exception e) {
			response.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			response.setMessage("操作失败，请联系管理员~");
			logger.error(" 成为代理商错误 "+e);
		}
		
		return JsonUtil.toJson(response);
	}
	
	/**
	 * 查看客户投诉建议
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/advice_list", method=RequestMethod.GET, produces="text/html;charset=UTF-8")
	public String getAdviceMessage(HttpServletRequest request,ModelMap model) {
		User user =(User)request.getSession().getAttribute("userSession");
		if(user==null||StringUtil.isEmpty(user.getOpenid())
				||StringUtil.isEmpty(user.getUnionid())
				){
			// 不构成支付条件
			model.put("appid", WechatPayProperties.getAppId());
			model.put("redirectUri", WechatPayProperties.getRedirectUri()+"?page=wechat/advice_list.html");
			return "wechat/index2"; 
		}
		
		return "wechat/advice/advice_list";
	}
	
	/**
	 * 查看客户投诉建议列表
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/advice_list_data", method=RequestMethod.GET, produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getAdviceMessageList(HttpServletRequest request,ModelMap model) {
		User user =(User)request.getSession().getAttribute("userSession");
		BootStrapReturn<AdviceMessage> result = new BootStrapReturn<AdviceMessage>();
		// 判断：客户只能查看自己；客服能查看所有信息
		try {
			Integer currentPage = Integer.parseInt(request.getParameter("numPerPage"));
			Integer numPerPage = Integer.parseInt(request.getParameter("pageNum"));
			PageQuery pageQuery = new PageQuery(numPerPage,currentPage);
			pageQuery.setIsQueryTotal(true); 
			List<AdviceMessage> list = AMService.getAdviceMessageListByUserId(user.getId(), pageQuery);
			result.setPage(pageQuery.getPageIndex());
			result.setTotal(pageQuery.getTotalCount());
			result.setRows(list);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		
		return JsonUtil.toJson(result);
	}
	
	
	@RequestMapping("/invite") 
	public String inviteInfo(HttpServletRequest request, ModelMap model) {
		User user =(User)request.getSession().getAttribute("userSession");
		// 订单信息
		if(user==null||StringUtil.isEmpty(user.getOpenid())
				||StringUtil.isEmpty(user.getUnionid())
				){
			// 不构成支付条件
			model.put("appid", WechatPayProperties.getAppId());
			model.put("redirectUri", WechatPayProperties.getRedirectUri()+"?page=wechat/invite.html");
			return "wechat/index2"; 
		}
		// 查询玩家信息
		User newUser = userService.getById(user.getId());
		List<User> userList =userService.getUserListByParentId(user.getId());
		Double profitFee = tradeFlowService.getTradeFlowTotal(newUser.getId(), null);
		model.put("profitFee", profitFee);
		model.put("playerList", userList);
		model.put("newUser", newUser);
		return "wechat/advice/invite_info";
	}
	
	/**
	 * 苹果操作指引
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/ios_guide") 
	public String ios_guide(HttpServletRequest request, ModelMap model) {
		
		return "wechat/advice/ios_guide";
	}
	
}
